﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Management;


namespace NetworkCrashSimulator
{
    public partial class FormMain : Form
    {
        private Core.NetworkAdapter ActualNetworkAdapter { get; set; }

        public FormMain()
        {
            InitializeComponent();
        }

        private void FormMain_Load(object sender, EventArgs e)
        {
            GetNetworkAdapters();
        }

        private void GetNetworkAdapters()
        {
            Core.NetworkAdapterConfiguration.NetworkAdapterConfigurationCollection networkAdapters = Core.NetworkAdapterConfiguration.GetInstances();

            foreach (Core.NetworkAdapterConfiguration networkAdapter in networkAdapters)
            {
                cbxNetworkAdapters.Items.Add(new ComboItem(networkAdapter, networkAdapter.Description));
            }
        }

        private Core.NetworkAdapterConfiguration GetSelectedNetworkAdapter()
        {
            ComboItem selectedItem = (ComboItem)cbxNetworkAdapters.SelectedItem;
            return (Core.NetworkAdapterConfiguration)selectedItem.BindingValue;
        }

        private void cbxNetworkAdapters_SelectedIndexChanged(object sender, EventArgs e)
        {
            GetNetworkAdapterInformation(GetSelectedNetworkAdapter());
        }

        private void GetNetworkAdapterInformation(Core.NetworkAdapterConfiguration networkAdapter)
        {
            ClearNetworkAdapterInformation();

            if (networkAdapter.IPAddress != null)
            {
                foreach (string ipAdress in networkAdapter.IPAddress)
                {
                    lblNameValue.Text += ipAdress;
                }
            }
            lblMacAddressesValue.Text = networkAdapter.MACAddress;
        }

        private void ClearNetworkAdapterInformation()
        {
            lblNameValue.Text = string.Empty;
            lblMacAddressesValue.Text = string.Empty;
        }

        private void btnCrash_Click(object sender, EventArgs e)
        {
            if (CheckSelectedNetworkAdapter())
            {
                if (MessageBox.Show("Do you really want to crash the selected network adapter?", "Crash Network Adapter", MessageBoxButtons.YesNo).Equals(DialogResult.Yes))
                {
                    CrashNetworkAdapter();
                }
            }
        }

        private void CrashNetworkAdapter()
        {
            try
            {
                Core.NetworkAdapterConfiguration networkAdapter = GetSelectedNetworkAdapter();
                networkAdapter.ReleaseDHCPLease();
                //TODO: Insert here the method to disable the network adapter.
                MessageBox.Show("Network Adapter Crashed.", "Success");
            }
            catch (Exception ex)
            {
                MessageBox.Show("Problems crashing the Network Adapter: " + ex.Message, "Error");
            }
        }

        private void btnRepair_Click(object sender, EventArgs e)
        {
            if (CheckSelectedNetworkAdapter())
            {
                RepairNetworkAdapter();
            }
        }

        private void RepairNetworkAdapter()
        {
            try
            {
                Core.NetworkAdapterConfiguration networkAdapter = GetSelectedNetworkAdapter();
                networkAdapter.RenewDHCPLease();
                //TODO: Insert here the method to enable the network adapter.
                MessageBox.Show("Network Adapter Repaired.", "Success");
            }
            catch (Exception ex)
            {
                MessageBox.Show("Problems repairing the Network Adapter: " + ex.Message, "Error");
            }
        }

        private bool CheckSelectedNetworkAdapter()
        {
            bool valid = true;

            if (cbxNetworkAdapters.SelectedItem == null)
            {
                MessageBox.Show("Select a Network Adapter!", "Error");
                valid = false;
            }

            return valid;
        }
    }
}